package com.abel.ignite.parser;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Iterator;
import java.util.Set;

/**
 * @author xiachao
 * @date 2019-04-29
 */

public class DMLParser {

    public static String parseInsert(String cacheName, JSONObject json){
        if(StringUtils.isEmpty(cacheName) || json == null
                || CollectionUtils.isEmpty(json.keySet())){
            return null;
        }
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO ");
        sql.append(cacheName);
        String [] bodys = parseJsonBody(json);
        sql.append(bodys[0]);
        sql.append(" VALUES ");
        sql.append(bodys[1]);
        return sql.toString();
    }

    public static String parseUpdate(String cacheName, JSONObject json){
        if(StringUtils.isEmpty(cacheName) || json == null
                || CollectionUtils.isEmpty(json.keySet())){
            return null;
        }
        if(!json.containsKey("id")){
            return null;
        }
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE ");
        sql.append(cacheName);
        sql.append(" SET ");
        String pairs = parseUpdatePair(json);
        sql.append(pairs);
        sql.append(" WHERE id = ");
        String id = json.getString("id") == null ? json.getString("ID") : "-1";
        sql.append(id);
        return sql.toString();
    }

    private static String [] parseJsonBody(JSONObject json){
        String [] result = new String[2];
        Set<String> keSet = json.keySet();
        Iterator<String> iterator = keSet.iterator();
        StringBuilder keyStr = new StringBuilder("(");
        StringBuilder valStr = new StringBuilder("(");
        while(iterator.hasNext()){
            String key = iterator.next();
            keyStr.append(key);
            keyStr.append(",");
            valStr.append(json.getString(key));
            valStr.append(",");
        }
        keyStr.replace(keyStr.length()-1, keyStr.length(), "");
        valStr.replace(keyStr.length()-1, keyStr.length(), "");
        keyStr.append(")");
        valStr.append(")");
        result[0] = keyStr.toString();
        result[1] = valStr.toString();
        return result;
    }

    private static String parseUpdatePair(JSONObject json){
        Set<String> keSet = json.keySet();
        Iterator<String> iterator = keSet.iterator();
        StringBuilder pairs = new StringBuilder();
        while(iterator.hasNext()){
            String key = iterator.next();
            if("id".equalsIgnoreCase(key)){
                continue;
            }
            pairs.append(key);
            pairs.append("=");
            pairs.append(json.getString(key));
            pairs.append(",");
        }
        pairs.replace(pairs.length()-1, pairs.length(), "");
        return pairs.toString();
    }

    public static void main(String[] args) {
        JSONObject json = new JSONObject();
        json.put("id", "2");
        json.put("name", "jack");
        String sql = DMLParser.parseUpdate("Person", json);
        System.out.println(sql);


    }

}
